import pandas as pd
import re
import warnings
import logging

from Determining_ad.src.Tools.df_column import ad_num

from Determining_ad.src.Others_ad.others_log_start import Others_ad_log_start

warnings.filterwarnings("ignore", category=FutureWarning)

'''
连接手机、获取日志、断开日志
'''


# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')


class HN_ad_log_start(Others_ad_log_start):
    def __init__(self):
        """
        调用父类的构造方法
        """
        super().__init__()
        self.title = "加密"

    def get_all_df(self, all_df):
        super().get_all_df(all_df)

    def get_ad_df(self, all_df):
        """
        获取ad_df
        :param all_df:
        :return: ad_df
        """
        # err_row = all_df.query('Level == "E"')
        # if err_row.empty:
        #     logging.info('没有E级别的日志')
        # else:
        #     err_row_message = err_row['Message']
        #     logging.info(f'err_row_message: \n{err_row_message}')
        # logging.info(f'all{all_df.to_string()}')

        ad_pattern2 = r'adId\[(.*?)\]解密后\[(.*?)\],\s*state\[(.*?)\],\s*adParam\[(.*?)\],\s*adOrderNo\[(.*?)\],\s*adType\[(.*?)\]解密后\[(.*?)\],\s*sp\[(.*?)\]\s*,\s*img\[(.*?)\](?:=*$|$)'
        data_list = []
        index_list = []
        for index, row in all_df.iterrows():
            # logging.info(f'开始处理message: {row["Message"]}')
            if ' 广告事件' in row["Message"]:
                matches = re.findall(ad_pattern2, row["Message"])
                # logging.info(f'匹配结果matches: {matches}')
                if matches:
                    # logging.info(f'匹配成功的message: {row["Message"]}')
                    timestamp = row["Timestamp"]
                    (not_unlock_adId, adId, chinese_adType, adParam, adOrderNo, not_unlock_adType, adType, sp, img) = matches[0]
                    data_list.append([timestamp, not_unlock_adId, adId, chinese_adType, adParam, adOrderNo, adType, sp, img])
                    index_list.append(index)
        # 将数据转换为DataFrame
        columns = ['Timestamp', 'not_unlock_adId', 'adId', 'State', 'adParam', 'adOrderNo', 'adType', 'sp', 'img']
        df = pd.DataFrame(data_list, columns=columns, index=index_list)
        self.ad_df = ad_num(df)
        self.ad_df = df[df['adId'] != '400']
        return self.ad_df

    def get_entity(self, entity):
        """
        给主体属性赋值
        :param entity:
        :return:
        """
        super().get_entity(entity)

    def get_phone_brand(self, phone_brand):
        super().get_phone_brand(phone_brand)

    def get_main_ad(self, ad_df):
        first_ad = super().get_main_ad(ad_df)
        return first_ad

    def get_pool_ad(self, ad_df):
        pool_df = super().get_pool_ad(ad_df)
        return pool_df

    def get_quit_ad(self, ad_df):
        quit_df = super().get_quit_ad(ad_df)
        return quit_df

    def get_second_quit_ad(self, ad_df):
        quit_second_df = super().get_second_quit_ad(ad_df)
        return quit_second_df

    def get_second_ad(self, ad_df):
        second_df = super().get_second_ad(ad_df)
        return second_df

    def get_video_ad(self, ad_df):
        video_df = super().get_video_ad(ad_df)
        return video_df

    def get_video50_ad(self, ad_df):
        video50_df = super().get_video50_ad(ad_df)
        return video50_df


# 实例化一个实例
hn_ad_log_start = HN_ad_log_start()
